package au.com.handsunited.web.controller.user;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import au.com.handsunited.exception.business.UsernameExistsException;
import au.com.handsunited.model.User;
import au.com.handsunited.service.UserService;
import au.com.handsunited.web.command.user.UserRegistrationCommand;
import au.com.handsunited.web.session.SessionSupport;

public class UserRegistrationController extends SimpleFormController {

	private UserService userService;


	private static Logger logger = LoggerFactory.getLogger(UserRegistrationController.class);


	/**
	 *
	 */
	public UserRegistrationController() {
		setCommandClass(UserRegistrationCommand.class);
	}


	/**
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {

		UserRegistrationCommand userRegistrationCommand = (UserRegistrationCommand) command;
		ModelAndView mav = null;

		User user = userRegistrationCommand.getUser();

		try {
			user = userService.createUser(user);
			SessionSupport.setLoggedInUser(request, user);

			Map<String, String> model = new HashMap<String, String>();
			model.put("userId", user.getId().toString());
			model.put("userName", user.getName());

			mav = new ModelAndView(getSuccessView(), model);

		} catch (UsernameExistsException e) {
			mav = new ModelAndView(getFormView());
		}

		return mav;
	}


/*
 	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		MultipartHttpServletRequest multipartRequest;

		try {
			multipartRequest = multipartResolver.resolveMultipart(request);
		} catch (MultipartException e) {

			ModelAndView mav = new ModelAndView(getFormView());
			mav.addObject("userCommand", command);
			errors.addError(new FieldError("userCommand", "image", null, true, null, null, "Image too big"));
			return mav;
		}

		UserCommand userCommand = (UserCommand) command;
		User user = userCommand.getUser();

		MultipartFile image = userCommand.getImage();

		if (image != null) {
			String imgSrc = processImage(image, user);
			if (StringUtils.isNotBlank(imgSrc)) {
				user.setImgSrc(imgSrc);
			}
		}

		try {
			userService.createUser(user);
		} catch (UsernameExistsException e) {
			//	add error here
		}

		ModelAndView mav = new ModelAndView(getSuccessView());
		mav.addObject("user", user);
		return mav;
	}
*/

/*
	private String processImage(MultipartFile image, User user) {

		String imgName = image.getOriginalFilename().toLowerCase();
		File imageFile = new File(imagesDir, imgName);

		try {
			image.transferTo(imageFile);
			return imgName;
		} catch (IllegalStateException e) {
			logger.error("Error while uploading image[{}] from User[{}]", image.getOriginalFilename(), user.getName());
			logger.error("", e);
		} catch (IOException e) {
			logger.error("Error while uploading image[{}] from User[{}]", image.getOriginalFilename(), user.getName());
			logger.error("", e);
		}

		return null;
	}

*/



	/**
	 *
	 * @param userService
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}


}
